/**
 * Created by Administrator on 2016/9/19 0019.
 */
$(function () {
    $('#province').each(function () {
        var url="/javascripts/area.json";
        var areaJson;
        var temp;
        var oprovince=$(this).find('.sel_pro');
        var ocity=$(this).find('.sel_city');
        var odistrict=$(this).find('.sel_street');
        //获取json数组
        $.getJSON(url,function (data) {
            areaJson=data;
            province();
        });
        //赋值省
        var  province=function () {
            $.each(areaJson,function(i,sel_pro){
                temp+="<option value='"+sel_pro.name+"'>"+sel_pro.name+"</option>";
            });
            // oprovince.html("请选择省份");

            oprovince.html(temp);
            city();
        };
        //赋值市

        var city = function(){
            temp = "";
            var n = oprovince.get(0).selectedIndex;

            if('北京天津上海重庆香港台湾澳门'.indexOf(areaJson[n].name) >= 0 ){

                ocity.css("display","none");
            }else{
                ocity.css("display","inline");
            }

            $.each(areaJson[n].city,function(i,sel_city){
                temp+="<option value='"+sel_city.name+"'>"+sel_city.name+"</option>";
            });
            // ocity.html("请选择市");
            ocity.html(temp);
            district();
        };
        var district=function () {
            temp = "";
            var m = oprovince.get(0).selectedIndex;
            var n = ocity.get(0).selectedIndex;
            if(typeof(areaJson[m].city[n].area) == "undefined"){
                odistrict.css("display","none");
            }else{
                odistrict.css("display","inline");

                $.each(areaJson[m].city[n].area,function(i,district){
                    temp+="<option value='"+district+"'>"+district+"</option>";
                });
                odistrict.html(temp);
            };
        }
        //选择省改变市
        oprovince.change(function(){
            city();
        });
        //选择市改变县
        ocity.change(function(){
            district();
        });

    })


})